package com.gzy.mylog.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gzy.mylog.R;

import org.w3c.dom.Text;

public class TabbarItem extends RelativeLayout {

    private int titleColor_n = Color.RED;
    private int titleColor_h = Color.BLUE;


    private String title;

    private Drawable icon_n;
    private Drawable icon_h;

    ImageView iv_icon;
    TextView tv_title;

    public TabbarItem(Context context) {
        super(context);
        init(null, 0);
    }

    public TabbarItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public TabbarItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.TabbarItem, defStyle, 0);

        title = a.getString(
                R.styleable.TabbarItem_title);

        if (a.hasValue(R.styleable.TabbarItem_icon_n)) {
            icon_n = a.getDrawable(
                    R.styleable.TabbarItem_icon_n);
            icon_n.setCallback(this);
        }
        if (a.hasValue(R.styleable.TabbarItem_icon_h)) {
            icon_h = a.getDrawable(
                    R.styleable.TabbarItem_icon_h);
            icon_h.setCallback(this);
        }


        titleColor_n = a.getColor(
                R.styleable.TabbarItem_titleColor_n,
                titleColor_n);
        titleColor_h = a.getColor(
                R.styleable.TabbarItem_titleColor_h,
                titleColor_h);

        a.recycle();


        LayoutInflater.from(getContext()).inflate(R.layout.layout_tabbar_item, this, true);


        iv_icon = findViewById(R.id.iv_icon);
        tv_title = findViewById(R.id.tv_title);

        iv_icon.setImageDrawable(icon_n);
        tv_title.setText(title);
        tv_title.setTextColor(titleColor_n);

    }


    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected){
            iv_icon.setImageDrawable(icon_h);
            tv_title.setTextColor(titleColor_h);
        }else{
            iv_icon.setImageDrawable(icon_n);
            tv_title.setTextColor(titleColor_n);
        }
    }

}
